package com.train.system.mapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.train.common.model.page.PageSorted;
import com.train.common.resp.PageResp;
import com.train.mybatis.base.MyBaseMapper;
import com.train.system.dto.carriage.TrainCarriageDTO;
import com.train.system.dto.carriage.TrainCarriagePageDTO;
import com.train.system.po.TrainCarriagePO;
import com.train.system.resp.carriage.TrainCarriageDetailResp;
import com.train.system.resp.carriage.TrainCarriageListResp;
import com.train.system.resp.carriage.TrainCarriagePageResp;

import java.util.List;

/**
 * @author baitiaojun
 * @description 针对表【trainCarriage(火车车厢)】的数据库操作Mapper
 * @createDate 2025-02-20 18:52:37
 * @Entity generator.domain.TrainCarriage
 */
public interface TrainCarriageMapper extends MyBaseMapper<TrainCarriagePO> {

    default PageResp<TrainCarriagePageResp> pageList(TrainCarriagePageDTO trainCarriagePageDTO) {
        LambdaQueryWrapper<TrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(trainCarriagePageDTO.getCode())) {
            queryWrapper.like(TrainCarriagePO::getTrainCode, trainCarriagePageDTO.getCode());
        }
        PageResp<TrainCarriagePO> trainCarriagePOPageResp = this.selectPage(
                trainCarriagePageDTO,
                queryWrapper, new PageSorted(TrainCarriagePO::getUpdateTime), new PageSorted(TrainCarriagePO::getCreateTime));
        List<TrainCarriagePageResp> trainCarriagePageRespList = BeanUtil.copyToList(trainCarriagePOPageResp.getData(), TrainCarriagePageResp.class);
        return new PageResp<>(trainCarriagePOPageResp.getTotal(), trainCarriagePageRespList);
    }

    default boolean existsId(Long id) {
        LambdaQueryWrapper<TrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainCarriagePO::getId, id);
        return this.exists(queryWrapper);
    }

    default List<TrainCarriageDTO> getTrainCarriageList(String trainCode) {
        LambdaQueryWrapper<TrainCarriagePO> queryWrapper
                = new LambdaQueryWrapper<TrainCarriagePO>()
                .select(TrainCarriagePO::getTrainCode,
                        TrainCarriagePO::getSeatType,
                        TrainCarriagePO::getRowCount,
                        TrainCarriagePO::getIndex)
                .eq(TrainCarriagePO::getTrainCode, trainCode);
        List<TrainCarriagePO> trainCarriagePOList = this.selectList(queryWrapper);
        return BeanUtil.copyToList(trainCarriagePOList, TrainCarriageDTO.class);
    }

    default boolean existsTrainCarriage(String trainCode, Integer carriageIndex) {
        LambdaQueryWrapper<TrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainCarriagePO::getTrainCode, trainCode)
                .eq(TrainCarriagePO::getIndex, carriageIndex);
        return this.exists(queryWrapper);
    }

    default List<TrainCarriageDTO> getIdCodeList(String trainCode) {
        LambdaQueryWrapper<TrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TrainCarriagePO::getId, TrainCarriagePO::getIndex)
                .eq(TrainCarriagePO::getTrainCode, trainCode);
        List<TrainCarriagePO> trainCarriagePOList = this.selectList(queryWrapper);
        return BeanUtil.copyToList(trainCarriagePOList, TrainCarriageDTO.class);
    }

    default TrainCarriageDetailResp getOne(TrainCarriageDTO trainCarriageDTO) {
        LambdaQueryWrapper<TrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TrainCarriagePO::getSeatType, TrainCarriagePO::getRowCount)
                .eq(StrUtil.isNotEmpty(trainCarriageDTO.getTrainCode()), TrainCarriagePO::getTrainCode, trainCarriageDTO.getTrainCode())
                .eq(ObjectUtil.isNotNull(trainCarriageDTO.getIndex()), TrainCarriagePO::getIndex, trainCarriageDTO.getIndex());
        TrainCarriagePO trainCarriagePO = this.selectOne(queryWrapper);
        return BeanUtil.copyProperties(trainCarriagePO, TrainCarriageDetailResp.class);
    }

    default List<TrainCarriageListResp> getTrainCarriageListByTrainCodes(List<String> trainCodes) {
        LambdaQueryWrapper<TrainCarriagePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TrainCarriagePO::getTrainCode, TrainCarriagePO::getIndex, TrainCarriagePO::getSeatType,
                        TrainCarriagePO::getSeatCount, TrainCarriagePO::getRowCount, TrainCarriagePO::getColCount)
                .in(TrainCarriagePO::getTrainCode, trainCodes);
        return BeanUtil.copyToList(this.selectList(queryWrapper), TrainCarriageListResp.class);
    }
}